# -*- coding:utf-8 -*-
import os
import sys
import xlsxwriter
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class MainTable(QTableWidget):

    def __init__(self):
        super(MainTable, self).__init__()
        self.setShowGrid(True)
        self.setStyleSheet( "QHeaderView::section{font-size:15px;font-weight: Bold;border-radius:14px;}")
        self.horizontalHeader().setDefaultSectionSize(120)
        self.verticalHeader().setDefaultSectionSize(30)

        self.head_list= ['部门','艺术家','项目名称','镜头号','任务类型','安排次数','完成次数','完成帧数','花费总工时']
        self.setColumnCount(len(self.head_list))
        self.setHorizontalHeaderLabels(self.head_list)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.setSelectionMode(QTableWidget.SingleSelection)
        self.customContextMenuRequested.connect(self.rightMenu_Main)
        self.data_list = []


    def init_items(self):
        self.clear()
        self.setHorizontalHeaderLabels(self.head_list)
        for r in range(self.rowCount()):
            for c in range(self.columnCount()):
                item = QTableWidgetItem()
                item.setText('')
                item.setTextAlignment(Qt.AlignCenter)
                self.setItem(r, c, item)

    def set_data(self,data_list):
        if data_list:
            self.data_list = data_list
            self.setRowCount(len(data_list))
            self.init_items()
            r = 0
            for i in data_list:
                for c in range(len(i)):
                    self.item(r,c).setText(str(i[c]))
                r += 1

            self.sortByColumn(0, Qt.AscendingOrder)


    def rightMenu_Main(self):
        menu = QMenu()
        filterAction = QAction(u'仅看该内容',self)
        filterAction.triggered.connect(self._filter_show)
        menu.addAction(filterAction)
        showAllAction = QAction(u'显示所有',self)
        showAllAction.triggered.connect(self._show_all)
        menu.addAction(showAllAction)
        excelAction = QAction(u'导出Excel',self)
        excelAction.triggered.connect(self._export_excel)
        menu.addAction(excelAction)
        menu.exec_(QCursor.pos())

    def _hidden_item(self,item):
        """
        根据传入的item,获取其列column与内容text，来
        过滤同列下所有内容不是该text的 item
        :return:
        """
        column = item.column()
        text = item.text()
        for r in range(self.rowCount()):
            c_item = self.item(r,column)
            c_text = c_item.text()
            if c_text != text:
                self.setRowHidden(r,True)

    def _filter_show(self):
        item = self.currentItem()
        if item:
            self._hidden_item(item)


    def _show_all(self):
        for r in range(self.rowCount()):
            self.setRowHidden(r,False)

    def _getShowDatas(self):

        show_row_datas = []
        for r in range(self.rowCount()):
            if not self.isRowHidden(r):
                current_row_data = []
                for c in range(self.columnCount()):
                    current_row_data.append(self.item(r,c).text())
                show_row_datas.append(current_row_data)

        return show_row_datas

    def _export_excel(self):
        data = self._getShowDatas()
        fileName = QFileDialog.getSaveFileName(None, "选择保存路径", ".", "XLSX(*.xlsx)")
        if fileName:
            excel_path = fileName[0]
            print(excel_path)
            wb = xlsxwriter.Workbook(excel_path)
            ws = wb.add_worksheet('工作量')
            header_style = wb.add_format({'align': 'center', 'font_size': 12,'bold': 1})
            content_style = wb.add_format({'align': 'center', 'font_size': 10,})
            ws.write_row(0,0,self.head_list,header_style)
            ws.set_column(0,len(data),25)
            for i in range(1,len(data)+1):
                ws.write_row(i,0,data[i-1],content_style)

            wb.close()


if __name__ == '__main__':
    data_list = [['07特效一', '王子维', 'G86(G86)', 'G8601_01_005', 'efx_xingmen', '1', '0', '0', '10.67'], 
                 ['07特效一', '邴楠', '山河表里(SHBL)', 'SHBL_test_mutayiyushuidejiaohu', 'efx', '1', '0', '0', '10.8'], 
                 ['07特效一', '黄锐', '致命愿望_BJ(ZMYW)', 'ZMYW10_01_0490', 'efx_beam', '1', '0', '0', '9.0'],
                 ['07特效一', '陈豪杰', '致命愿望_BJ(ZMYW)', 'ZMYW10_01_0520', 'efx_sand', '1', '0', '0', '23.0'], 
                 ['07特效一', '陈豪杰', '致命愿望_BJ(ZMYW)', 'ZMYW10_01_0530', 'efx_sand', '1', '0', '0', '9.0'],
                 ['07特效一', '杜江帆', '中国(ZG)', 'ZG_ZP_0011_1', 'efx_starrysky', '1', '0', '0', '9.75'], 
                 ['07特效一', '张谷雷', '非制作项目(NON)', 'G86尾焰', '自定义', '1', '-', '-', '0.0'], 
                 ['07特效一', '陈钿钿', '中国(ZG)', 'ZG_沙盘或星空测试返修预留', '自定义', '1', '-', '-', '0.0']]
    app = QApplication(sys.argv)
    m = MainTable()
    m.set_data(data_list)
    m.show()
    sys.exit(app.exec_())
    